﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace PTA.NET
{
    public partial class FrmConfirm : DevExpress.XtraEditors.XtraForm
    {
        public FrmConfirm()
        {
            InitializeComponent();
            CustomerItem = new Customer();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXuLy_Click(object sender, EventArgs e)
        {
            XuLy();
        }

        public Customer CustomerItem;

        public void XuLy()
        {
            // khai báo các mảng data cho các biểu đồ
            MyData[] B1 = new MyData[Common.B1.Count()];
            MyData[] B2a = new MyData[Common.B2a.Count()];
            MyData[] B2b = new MyData[Common.B2b.Count()];
            MyData[] B3 = new MyData[Common.B3.Count()];
            MyData[] B4 = new MyData[Common.B4.Count()];
            MyData[] B5 = new MyData[Common.B5.Count()];
            MyData[] B6 = new MyData[Common.B6.Count()];

            // Điểm tạm
            int? diem_tho;
            int? diem;
            //int diemChuyen = 0;
            int? DiemK = 0;
            string GioiTinh = CustomerItem.GioiTinh ? "NAM" : "NU";

            // Load danh sách bảng chuyển điểm
            ChuyenDiem objChuyenDiem = new ChuyenDiem(GioiTinh);



            ///////////////////////////////////////////////////////////////////////////////////////
            //  B1  
            ///////////////////////////////////////////////////////////////////////////////////////
            for (int i = 0; i < Common.B1.Count(); i++)
            {
                diem_tho = CauTaoCacThang.TinhDiem3(GioiTinh, CustomerItem.QuestionList, Common.B1[i]);

                // Lưu lại điểm thô K
                if (Common.B1[i] != null && Common.B1[i].Equals("K")) DiemK = diem_tho;

                // Tính các điểm đặc biệt với Hs, Pd, Pt,...
                diem = objChuyenDiem.TraDiem(GioiTinh, diem_tho, Common.B1[i], (int)DiemK);                

                MyData mData = new MyData(i, Common.B1[i], diem_tho, diem);
                B1[i] = mData;
            }

            ///////////////////////////////////////////////////////////////////////////////////////
            //  B2a  
            ///////////////////////////////////////////////////////////////////////////////////////
            for (int i = 0; i < Common.B2a.Count(); i++)
            {
                diem_tho = CauTaoCacThang.TinhDiem3(GioiTinh, CustomerItem.QuestionList, Common.B2a[i]);

                // Chuyen diem, tra diem
                diem = objChuyenDiem.TraDiem(GioiTinh, diem_tho, Common.B2a[i]);
                MyData mData = new MyData(i, Common.B2a[i], diem_tho, diem);
                B2a[i] = mData;
            }

            ///////////////////////////////////////////////////////////////////////////////////////
            //  B2b  
            ///////////////////////////////////////////////////////////////////////////////////////
            for (int i = 0; i < Common.B2b.Count(); i++)
            {
                diem_tho = CauTaoCacThang.TinhDiem3(GioiTinh, CustomerItem.QuestionList, Common.B2b[i]);

                // Chuyen diem, tra diem
                diem = objChuyenDiem.TraDiem(GioiTinh, diem_tho, Common.B2b[i]);

                MyData mData = new MyData(i, Common.B2b[i], diem_tho, diem);
                B2b[i] = mData;
            }

            ///////////////////////////////////////////////////////////////////////////////////////
            //  B3  
            ///////////////////////////////////////////////////////////////////////////////////////
            for (int i = 0; i < Common.B3.Count(); i++)
            {
                diem_tho = CauTaoCacThang.TinhDiem3(GioiTinh, CustomerItem.QuestionList, Common.B3[i]);

                // Chuyen diem, tra diem
                diem = objChuyenDiem.TraDiem(GioiTinh, diem_tho, Common.B3[i]);

                MyData mData = new MyData(i, Common.B3[i], diem_tho, diem);
                B3[i] = mData;
            }

            ///////////////////////////////////////////////////////////////////////////////////////
            //  B4
            ///////////////////////////////////////////////////////////////////////////////////////
            for (int i = 0; i < Common.B4.Count(); i++)
            {
                diem_tho = CauTaoCacThang.TinhDiem3(GioiTinh, CustomerItem.QuestionList, Common.B4[i]);

                // Chuyen diem, tra diem
                diem = objChuyenDiem.TraDiem(GioiTinh, diem_tho, Common.B4[i]);

                MyData mData = new MyData(i, Common.B4[i], diem_tho, diem);
                B4[i] = mData;
            }

            ///////////////////////////////////////////////////////////////////////////////////////
            //  B5
            ///////////////////////////////////////////////////////////////////////////////////////
            for (int i = 0; i < Common.B5.Count(); i++)
            {
                diem_tho = CauTaoCacThang.TinhDiem3(GioiTinh, CustomerItem.QuestionList, Common.B5[i]);

                // Chuyen diem, tra diem
                diem = objChuyenDiem.TraDiem(GioiTinh, diem_tho, Common.B5[i]);

                MyData mData = new MyData(i, Common.B5[i], diem_tho, diem);
                B5[i] = mData;
            }

            ///////////////////////////////////////////////////////////////////////////////////////
            //  B6
            ///////////////////////////////////////////////////////////////////////////////////////
            for (int i = 0; i < Common.B6.Count(); i++)
            {
                diem_tho = CauTaoCacThang.TinhDiem3(GioiTinh, CustomerItem.QuestionList, Common.B6[i]);

                // Chuyen diem, tra diem
                diem = objChuyenDiem.TraDiem(GioiTinh, diem_tho, Common.B6[i]);

                MyData mData = new MyData(i, Common.B6[i], diem_tho, diem);
                B6[i] = mData;
            }



            ////////////////////////////////////////////////////////////////////////////////////////
            Report frmReport = new Report();
            try
            {
                frmReport.CreateData(CustomerItem.CustomerName, GioiTinh, CustomerItem.SinhNam, B1, B2a, B2b, B3, B4, B5, B6);
                frmReport.ShowDialog();
            }
            catch (Exception ex)
            {
            }
            
        }

        

        private void FrmConfirm_Load(object sender, EventArgs e)
        {
            lblHoTen.Text = CustomerItem.CustomerName;
            lblGioiTinh.Text = CustomerItem.GioiTinh ? "Nam" : "Nữ";
            imgGioiTinh.Image = CustomerItem.GioiTinh ? global::PTA.NET.Properties.Resources.male : global::PTA.NET.Properties.Resources.female;
            lblDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            ThongKe();
        }
        int Dung = 0, Sai = 0, Khong = 0;
        private void ThongKe()
        {
           
            for (int i = 1; i <= 567; i++) 
            {
                if (CustomerItem.QuestionList[i].Answer == null) continue;

                if (CustomerItem.QuestionList[i].Answer.ToLower().Equals("dung"))
                    Dung++;
                else if (CustomerItem.QuestionList[i].Answer.ToLower().Equals("sai"))
                    Sai++;
                else if (CustomerItem.QuestionList[i].Answer.ToLower().Equals("khong"))
                    Khong++;
            }

            lblTotalDUNG.Text = Dung.ToString();
            lblTotalKHONG.Text = Khong.ToString();
            lblTotalSAI.Text = Sai.ToString();

        }

        private void btnKHONG_Click(object sender, EventArgs e)
        {
            lblTotalKHONG.Text = (567 - int.Parse(lblTotalDUNG.Text) - int.Parse(lblTotalSAI.Text)).ToString();
            FillUnAnswer("KHONG");
        }

        private void btnSAI_Click(object sender, EventArgs e)
        {
            lblTotalSAI.Text = (567 - int.Parse(lblTotalDUNG.Text) - int.Parse(lblTotalKHONG.Text)).ToString();
            FillUnAnswer("SAI");
        }        

        private void btnDUNG_Click(object sender, EventArgs e)
        {
            lblTotalDUNG.Text = (567 - int.Parse(lblTotalSAI.Text) - int.Parse(lblTotalKHONG.Text)).ToString();
            FillUnAnswer("DUNG");
        }

        // Nhập các câu trả lời còn trống (chưa trả lời)
        private void FillUnAnswer(string TraLoi)
        {
            for (int i = 1; i <= 567; i++)
            {
            	if (CustomerItem.QuestionList[i].Answer == null)
                {
                    CustomerItem.QuestionList[i].Answer = TraLoi;
                }
            }
        }

    }
}